﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace TcpServer
{
    class Program
    {
        static void Main(string[] args)
        {
            // 服务端创建监听套接字
            Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // 设置IP地址和端口号
            IPAddress ipAddress = new IPAddress(new byte[] { 10, 3, 5, 21 });
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 8000);

            // 绑定ip和端口
            listenSocket.Bind(ipEndPoint);
            // 监听 并设置挂起的连接队列的最大长度
            listenSocket.Listen(100);

            // 接收连接
            Socket client = listenSocket.Accept();
            byte[] date = new byte[1024];
            while(true)
            {
                int length = client.Receive(date);
                string str = Encoding.UTF8.GetString(date);
                Console.WriteLine("客户端说：" + str);
            }

            client.Close();
            listenSocket.Close();

            
        }
    }
}
